package com.libweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libweb.model.dao.BoardDao;
import com.libweb.model.dao.MemberDao;
import com.libweb.model.dto.Board;
import com.libweb.model.dto.Member;

@WebServlet("/board/write.action")
public class BoardWriteServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		//1. 브라우저에 사용자가 입력한 데이터를 읽어서 변수에 저장 (요청 정보에서 데이터 읽기)
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		Member member = (Member)req.getSession().getAttribute("loginuser");
		
		Board board = new Board();
		board.setTitle(title);
		board.setWriter(member.getMemberId());
		board.setContent(content);
		
		//2. DAO 객체를 이용해서 데이터베이스에 데이터 저장
		BoardDao dao = new BoardDao();
		dao.insertBoard(board);
		
		//3. 목록 페이지로 이동
		resp.sendRedirect("/libweb/board/list.action");
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		doGet(req, resp);
	}
	
}






